<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\matchingRule\controller;

use plugin\saiadmin\basic\BaseController;
use app\matchingRule\logic\MatchingRuleLogic;
use app\matchingRule\validate\MatchingRuleValidate;
use support\Request;
use support\Response;

/**
 * 匹配规则控制器
 */
class MatchingRuleController extends BaseController
{
    /**
     * 数据表主键
     */
    protected $pk = 'rule_id';
    
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new MatchingRuleLogic();
        $this->validate = new MatchingRuleValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['rule_name_zh', ''],
            ['rule_name_en', ''],
            ['rule_name_ja', ''],
            ['rule_id', ''],
            ['rule_name_ko', ''],
            ['rule_type', ''],
        ]);
        $data = $this->logic->tree($where);
        return $this->success($data);
    }
    public function List(Request $request): Response{
        $where = $request->more([
            ['rule_level', 0]
        ]);
        $query = $this->logic->search($where)->field('rule_name_zh as label, rule_id as value');
        $data = $this->logic->getAll($query);
        if($data){
            array_unshift($data, ['label' => '请选择', 'value' => 0]);
        }else{
            $data = [['label' =>'请选择', 'value' => 0]];
        }
        return $this->success($data);
    }
    public function updateSortOrder(Request $request) : Response
    {
        $id = $request->input('id', '');
        $sort_order = $request->input('rule_sort', 1);
       
        $result = $this->logic->where($this->pk, $id)->update(['rule_sort' => $sort_order,'update_time'=>date('Y-m-d H:i:s')]);
          
        if ($result) {
            // $this->afterChange('changeStatus');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
    public function save(Request $request) : Response
    {
        $data = $request->post();
        if ($this->validate) {
            if (!$this->validate->scene('save')->check($data)) {
                return $this->fail($this->validate->getError());
            }
        }
        if($data['rule_level']==0){
            $count=$this->logic->search(['rule_level'=>0])->count();
            if($count>=10){
                return $this->fail('最多只能创建10个一级分类');
            }
        }
       
        if($data['parent_id'] && $data['parent_id'] == 0){
            $data['rule_type'] = $this->logic->search(['id'=>$data['parent_id']])->value('rule_type');       
        }
        $result = $this->logic->save($data);
        if ($result) {
            $this->afterChange('save');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }

    }
    public function update(Request $request, $id) : Response
    {
        $id = $request->input('id', $id);
        $data = $request->post();
        if ($this->validate) {
            if (!$this->validate->scene('update')->check($data)) {
                return $this->fail($this->validate->getError());
            }
        }
        $info = $this->logic->find($id);
        if (!$info) {
            return $this->fail('没有找到该数据');
        }
        if($data['rule_level']==0){
            $count=$this->logic->search(['rule_level'=>0])->where('rule_id','<>',$id)->count();
            if($count>=10){
                return $this->fail('最多只能创建10个一级分类');
            }
        }
        $result = $this->logic->update($data, [$this->pk => $id]);
        $this->logic->search(['parent_id'=>$id])->update(['rule_type' => $data['rule_type']]);       
        if ($result) {
            $this->afterChange('update');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

}
